package api

import (
	"encoding/json"
	"github.com/gin-gonic/gin"
	"go_api/model"
	"go_api/pkg/validator"
	"go_api/request"
)

type InterfaceController struct {
	BaseController
}

// 请求方法
var method = map[int]string{
	1: "GET",
	2: "POST",
	3: "PUT",
	4: "DELETE",
}

// 请求方法
var status = map[int]string{
	1: "未完成",
	2: "已完成",
}

// GetInterFaceList 获取所有分组数据
func (i InterfaceController) GetInterFaceList(c *gin.Context) {
	//数据验证
	var data request.GetInterfaceListRequest
	msg, ok := validator.ValidatorData(c, &data)
	if !ok {
		i.responseError(c, 1, msg)
		return
	}

	interfaceModel := model.XInterface{}
	items, total := interfaceModel.GetInterfaceLimitList(data.ProjectId, data.MenuId, data.Page, data.PageSize)
	for i := range items {
		items[i].MethodStr = method[items[i].Method]
		items[i].StatusStr = status[items[i].Status]
	}
	result := pageData{
		Items:      items,
		TotalCount: int(total),
		Limit:      data.PageSize,
	}
	i.responseSuccess(c, result)
}

// DelInterFace 删除接口
func (i InterfaceController) DelInterFace(c *gin.Context) {
	//获取当前登录人
	uId := c.GetInt("uId")
	//数据验证
	var data request.DelInterfaceRequest
	msg, ok := validator.ValidatorData(c, &data)
	if !ok {
		i.responseError(c, 1, msg)
		return
	}

	interfaceModel := model.XInterface{}
	//查询记录是否存在
	interfaceInfo, err := interfaceModel.GetInterfaceInfo(data.InterfaceId)
	if err != nil {
		i.responseError(c, 1, "查找失败")
		return
	}
	if interfaceInfo.ID <= 0 {
		i.responseError(c, 1, "接口不存在")
		return
	}
	if interfaceInfo.Author != uId {
		i.responseError(c, 1, "没有删除权限")
		return
	}
	if interfaceInfo.ProjectID != data.ProjectId {
		i.responseError(c, 1, "接口归属项目错误")
		return
	}
	//删除记录
	err = interfaceModel.DeleteInterface(interfaceInfo.ID)
	if err != nil {
		i.responseError(c, 1, "删除失败")
		return
	}
	i.responseSuccess(c, nil)
}

// CreateInterFace 添加接口
func (i InterfaceController) CreateInterFace(c *gin.Context) {

	//获取当前登录人
	uId := c.GetInt("uId")
	//数据验证
	var data request.CreateInterfaceRequest
	msg, ok := validator.ValidatorData(c, &data)
	if !ok {
		i.responseError(c, 1, msg)
		return
	}

	interfaceModel := model.XInterface{}
	interfaceModel.Method = data.Method
	interfaceModel.Name = data.Name
	interfaceModel.ProjectID = data.ProjectId
	interfaceModel.MenuID = data.MenuId
	interfaceModel.Path = data.Path
	interfaceModel.Author = uId

	err := interfaceModel.CreateInterface()
	if err != nil {
		i.responseError(c, 1, "添加失败")
		return
	}

	i.responseSuccess(c, nil)

}

// EditInterface 编辑接口
func (i InterfaceController) EditInterface(c *gin.Context) {

	//数据验证
	var data request.EditInterfaceRequest
	msg, ok := validator.ValidatorData(c, &data)
	if !ok {
		i.responseError(c, 1, msg)
		return
	}

	// 将数组数据序列化为JSON字符串
	bodyJson, _ := json.Marshal(data.Body)
	headerJson, _ := json.Marshal(data.Header)
	queryJson, _ := json.Marshal(data.Query)
	jsonData, _ := json.Marshal(data.Json)

	interfaceModel := model.XInterface{}
	interfaceModel.Method = data.Method
	interfaceModel.Name = data.Name
	interfaceModel.ProjectID = data.ProjectId
	interfaceModel.MenuID = data.MenuId
	interfaceModel.Path = data.Path
	interfaceModel.Body = string(bodyJson)
	interfaceModel.Header = string(headerJson)
	interfaceModel.Query = string(queryJson)
	interfaceModel.JSON = string(jsonData)

	err := interfaceModel.EditInterface(data.InterfaceId)
	if err != nil {
		i.responseError(c, 1, "添加失败")
		return
	}

	i.responseSuccess(c, nil)
}

func (i InterfaceController) GetInterfaceInfo(c *gin.Context) {
	//数据验证
	var data request.GetInterfaceRequest
	msg, ok := validator.ValidatorData(c, &data)
	if !ok {
		i.responseError(c, 1, msg)
		return
	}

	interfaceModel := model.XInterface{}
	//查询记录是否存在
	interfaceInfo, err := interfaceModel.GetInterfaceInfo(data.InterfaceId)
	if err != nil {
		i.responseError(c, 1, "查找失败")
		return
	}
	if interfaceInfo.ID <= 0 {
		i.responseError(c, 1, "接口不存在")
		return
	}
	if interfaceInfo.ProjectID != data.ProjectId {
		i.responseError(c, 1, "接口归属项目错误")
		return
	}
	var headerData []interface{}
	_ = json.Unmarshal([]byte(interfaceInfo.Header), &headerData)

	var bodyData []interface{}
	json.Unmarshal([]byte(interfaceInfo.Body), &bodyData)

	var queryData []interface{}
	json.Unmarshal([]byte(interfaceInfo.Query), &queryData)

	var jsonData []interface{}
	json.Unmarshal([]byte(interfaceInfo.JSON), &jsonData)

	result := make(map[string]interface{})
	result["id"] = interfaceInfo.ID
	result["menu_id"] = interfaceInfo.MenuID
	result["author"] = interfaceInfo.Author
	result["name"] = interfaceInfo.Name
	result["path"] = interfaceInfo.Path
	result["method"] = interfaceInfo.Method
	result["method_str"] = method[interfaceInfo.Method]
	result["header"] = headerData
	result["body"] = bodyData
	result["query"] = queryData
	result["json"] = jsonData

	i.responseSuccess(c, result)
}
